﻿using System;
using System.Linq;
using Tss.MagicWeb.Domain.Cards;
using Tss.MagicWeb.Domain.Entities;

namespace Tss.MagicWeb.Domain.Decks
{
    public class StandardDeckValidationRuleProvider : IDeckValidationRuleProvider
    {
        public Func<Deck, BasePhysicalCard, DeckValidationRuleResult> GetValidationRule()
        {
            // Check that deck does not contain more than 4 cards
            return
                (Deck x, BasePhysicalCard y) =>

                (
                    // There is no such cards in the deck...
                    (!x.DeckItems.Any(di => di.CardGuid.Equals(y.PhysicalCardGuid.ToString()))) ||
                    // ... or there are no more than 4 cards
                    (x.DeckItems.Any(di => di.CardGuid.Equals(y.PhysicalCardGuid.ToString())) &&
                    x.DeckItems.First(di => di.CardGuid.Equals(y.PhysicalCardGuid.ToString())).Amount <= 4))

                    ? DeckValidationRuleResult.Successful()
                    : new DeckValidationRuleResult(String.Format("Deck cannot contain more than 4 {0} cards", y.OracleName)
                );
        }
    }
}
